// Updated RolesContext.jsx with proper path matching for routes with parameters

import React, { createContext, useContext, useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';

const RolesContext = createContext();

export const RolesProvider = ({ children }) => {
  const location = useLocation();
  const [allowedRoles, setAllowedRoles] = useState([]);

  useEffect(() => {
    // Helper function to check if pathname matches a pattern with parameters
    const matchesPattern = (pattern) => {
      // Convert route pattern with params (:param) to regex
      const regexPattern = pattern
        .replace(/:[^/]+/g, '[^/]+') // Replace :param with regex for any characters except /
        .replace(/\//g, '\\/');       // Escape / characters
      
      const regex = new RegExp(`^${regexPattern}$`);
      return regex.test(location.pathname);
    };

    // Set roles based on current path
    
    // Original routes
    if (location.pathname === "/DashboardTest") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    } 
    else if (location.pathname === "/teams") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    } 
    else if (matchesPattern("/teams/:teamId")) {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/user-teams") {
      setAllowedRoles(["user", "superadmin"]);
    } 
    else if (location.pathname === "/card/add") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (matchesPattern("/plan/:planId/subscribe")) {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/plan") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/user/edit") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    } 
    else if (location.pathname === "/account/edit") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/users") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/create-user") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (matchesPattern("/edit-user/:userId")) {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (matchesPattern("/user/:userId")) {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    else if (location.pathname === "/subscriber") {
      setAllowedRoles(["admin", "superadmin"]);
    } 
    
    // Workspace routes
    else if (location.pathname === "/workspaces") {
      setAllowedRoles(["superadmin"]);
    } 
    else if (location.pathname === "/workspaces/create") {
      setAllowedRoles(["superadmin"]);
    } 
    else if (matchesPattern("/workspaces/edit/:id")) {
      setAllowedRoles(["superadmin"]);
    } 
    else if (matchesPattern("/workspaces/view/:id")) {
      setAllowedRoles(["superadmin"]);
    }
    
    // New SuperAdmin route for trial refresh
    else if (location.pathname === "/admin/refresh") {
      setAllowedRoles(["superadmin"]);
    }
    
    // Option 1: Payment Methods Management Routes
    else if (location.pathname === "/payment-methods") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    else if (matchesPattern("/payment-methods/:cardId/set-default")) {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    else if (matchesPattern("/payment-methods/:cardId/remove")) {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    
    // Option 2: Subscription Management Routes
    else if (location.pathname === "/subscription") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    else if (matchesPattern("/subscription/cancel/:subscriptionId")) {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    
    // Option 3: Payment History Routes
    else if (location.pathname === "/payment-history") {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    else if (matchesPattern("/payment-history/:invoiceId")) {
      setAllowedRoles(["admin", "user", "superadmin"]);
    }
    
  }, [location.pathname]);

  return (
    <RolesContext.Provider value={allowedRoles}>
      {children}
    </RolesContext.Provider>
  );
};

export const useRoles = () => {
  return useContext(RolesContext);
};